{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from datetime import datetime, date\n",
    "import pandas as pd\n",
    "from gs_quant.instrument import FXOption\n",
    "from gs_quant.common import BuySell, OptionType\n",
    "from gs_quant.backtests.triggers import PeriodicTrigger, PeriodicTriggerRequirements\n",
    "from gs_quant.backtests.actions import AddTradeAction\n",
    "from gs_quant.backtests.generic_engine import GenericEngine\n",
    "from gs_quant.backtests.strategy import Strategy\n",
    "from gs_quant.risk import Price"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize session\n",
    "from gs_quant.session import GsSession\n",
    "\n",
    "GsSession.use(client_id=None, client_secret=None, scopes=('run_analytics',))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Buy $100k 2y USDJPY ATMF call, roll monthly"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define backtest dates\n",
    "start_date = date(2021, 6, 1)\n",
    "end_date = datetime.today().date()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Define instrument for strategy\n",
    "\n",
    "# FX Option\n",
    "call = FXOption(\n",
    "    buy_sell=BuySell.Buy,\n",
    "    option_type=OptionType.Call,\n",
    "    pair='USDJPY',\n",
    "    strike_price='ATMF',\n",
    "    expiration_date='2y',\n",
    "    name='2y_call',\n",
    "    premium=0,\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Periodic trigger: based on frequency\n",
    "freq = '1m'\n",
    "trig_req = PeriodicTriggerRequirements(start_date=start_date, end_date=end_date, frequency=freq)\n",
    "\n",
    "# hold the trade for 1m\n",
    "actions = AddTradeAction(call, freq)\n",
    "\n",
    "# starting with empty portfolio (first arg to Strategy), apply actions on trig_req\n",
    "triggers = PeriodicTrigger(trig_req, actions)\n",
    "strategy = Strategy(None, triggers)\n",
    "\n",
    "# run backtest daily\n",
    "GE = GenericEngine()\n",
    "backtest = GE.run_backtest(strategy, start=start_date, end=end_date, frequency='1b', show_progress=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# View backtest trade ledger\n",
    "backtest.trade_ledger()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# View results summary\n",
    "backtest.result_summary"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# View Mark to Market\n",
    "pd.DataFrame({'Generic backtester': backtest.result_summary[Price]}).plot(figsize=(10, 6), title='Mark to market')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# View Performance\n",
    "pd.DataFrame({'Generic backtester': backtest.result_summary['Cumulative Cash'] + backtest.result_summary[Price]}).plot(\n",
    "    figsize=(10, 6), title='Performance'\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
